package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    @Transactional(propagation = Propagation.REQUIRED)
    public  int add(){
        // 1. 非空判断
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername("奥巴马");
        userInfo.setPassword("45");
        // 2. 调用 service 执行添加
        int result = userService.add(userInfo);
        System.out.println("result:"+result);

        userService.insert(userInfo);
//        try{
//            int num = 10/0;
//        }catch (Exception e){
////            throw e;
//            // 手动回滚
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//        }

        // 3. 将结果给前端
        return 0;
    }
}
